var nodeContent, linkPanel, linkTpl, infoPanel, toolbar, labyrinthState, pauseMask;
var lowerPanel, linkList, alertList;

function initialize() {
    
}

/*
** This function sends an Ajax request and parses the returned XML, which
** contains all of the information relevant to this node. Helper functions
** are used to redraw the links and counters.
*/
function nodeUpdated(data) {

    // Clear the current list of alerts and then add the new ones.
    $('#node_alerts ul').children().detach();
    if(data.alerts.length)
        $(data.alerts).each(addAlert);
    
    // Clear the current list of links
    $('#node_links ul').children().detach();
    
    // Add the outbound links, unless there are none,
    // which means that we're at the end of the labyrinth.
    if(data.links.length) {
        $(data.links).each(addLink);
        
        // Unlock the UI
        nodeMask.dialog('close');
        
        // Restart the state checker
        checkLabyrinthState.start();
    }
    else {
        // Unlock the UI
        nodeMask.dialog('close');
        
        // Display a dialog to choose between the report and returning home.
        $('<div></div>')
    		.html('You have completed the labyrinth. You can now view your results or return home.')
    		.dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				'View Report': function() {
					$(this).dialog('close');
					document.location = siteRoot;
				},
				'Return Home': function() {
					$(this).dialog('close');
					document.location = siteRoot;
				}
			}
		});
		
        // Stop the state check and clock updates
        checkLabyrinthState.stop();
    }
    
    // Display the node title
    $('#node_title').html(data.title);
    
    // Set the browser window's title to the node's title
    document.title = data.title;
    
    // Display the node message
    $('#node_message').html(data.message);
}

function addLink(index, link) {
    if(link.icon)
        link.icon = '<img src="'+link.icon+'" />';
    $('#node_links ul').append('<li><a href="javascript:loadNode('+link.destination+');">'+link.icon+link.label+'</a></li>');
}

function addAlert(index, alert) {
    $('#node_alerts ul').append('<li>'+alert.text()+'</li>');
}

function addCounter(index, counter) {
    
}

function labyrinthComplete(decision) {
    if(decision == 'ok')
        document.location = siteRoot;
    else
        document.location = siteRoot;
}

function stateUpdated(data){
    // Update the clock
    var minutes, seconds;
    // Some logic used to handle timeouts, as well as time formatting
    if(data.timeRemaining > 0) {
        if(data.timeRemaining%60 < 10) {
            minutes = Math.floor(data.timeRemaining/60);
            seconds = '0'+data.timeRemaining%60;
        }
        else {
            minutes = Math.floor(data.timeRemaining/60);
            seconds = data.timeRemaining%60;
        }
        // Update the time
        $('#minutes_remaining').html(minutes);
        $('#seconds_remaining').html(seconds);
        data.timeRemaining--;
        
    } else if(data.timeRemaining <= 0) {
        
        // Display a dialog to choose between the report and returning home.
        $('<div></div>')
    		.html('The maximum time allowed for this labyrinth has been exceeded.')
    		.dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				'Ok': function() {
					$(this).dialog('close');
					document.location = siteRoot;
				}
			}
		});

        // Stop the state check and clock updates
        checkLabyrinthState.stop();
    }
    
    if(data.paused == 1) {
        pauseMask = new Ext.LoadMask(Ext.getBody(), {msg:data.message, msgCls: 'lockState'});
        pauseMask.show();
    }
    else {
        if(pauseMask) {
            pauseMask.hide();
            pauseMask = undefined;
            loadNode(currentNodeID);
        }
    }
}